//logs.js
import WxValidate from '../../utils/WxValidate.js'

const util = require('../../utils/util.js')
const app = getApp()

let Validate = ""

Page({
  data: {
    logs: [],
    form: {
      login: '',
      password: ''
    }
  },
  onLoad: function () {
    if (!!wx.getStorageSync('sale_token')) {
      // console.log(23)
      wx.reLaunch({
        url: '../index/index'
      })
    }
    let _this = this;
    // console.log(app)
    const rules = {
      login: {
        required: true,
        tel: true
      },
      password: {
        required: true
      }
    }
    
    const messages = {
      login: {
        required: '请输入手机号码',
        tel: '请输入正确的手机号码'
      },
      password: {
        required: '请输入密码',
      }
    }
    Validate = new WxValidate(rules, messages)
    
  },
  formSubmit: function (e) {
    const params = e.detail.value
    
    if (!Validate.checkForm(e)) {

      const error = Validate.errorList[0]
      app.showToast(error.msg ,false ,500 , '../../assets/warning.png')
      //提示信息  
      console.log(error,e)
      return false
    }   
    
    app.query('/api/sessions' , e.detail.value , 'post').then(res => {
      // console.log(res)
      if (res.status == 'success') {
        // app.showModel({msg: '登录成功'}) 
        app.showToast('登录成功', 'success', 500)
        wx.setStorageSync('current_user', res.current_user)  
        wx.setStorageSync('materials', res.materials)
        wx.setStorageSync('sale_token', res.token)
        wx.reLaunch({
          url: '../index/index'
        })
        
      }else{
        app.showToast('手机号或者密码不正确', false, 500, '../../assets/warning.png')
      }
    });
  },
})
